package com.zxjbyte.yiyi.module.system.config;

import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import com.zxjbyte.yiyi.module.system.context.properties.CaptchaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 系统模板公共配置类
 *
 * @Author zhangxingjia
 * @Date 2024/8/14 10:56
 * @Version: 1.0
 */
@Configuration
public class SystemCommonConfig {

    @Resource
    private CaptchaProperties captchaProperties;

    /**
     * 验证码生成器
     * @return
     */
    @Bean
    public CodeGenerator captchaCodeGenerator(){
        CodeGenerator codeGenerator;
        if(captchaProperties.getMode().equals(CaptchaProperties.CaptchaMode.MATH)){
            codeGenerator = new MathGenerator();
        } else {
            String baseStr = null;
            if(captchaProperties.getMode().equals(CaptchaProperties.CaptchaMode.RANDOM_CHAR_NUMBER)){
                baseStr = RandomUtil.BASE_CHAR_NUMBER;
            } else if(captchaProperties.getMode().equals(CaptchaProperties.CaptchaMode.RANDOM_CHAR)){
                baseStr = RandomUtil.BASE_CHAR.toUpperCase() + RandomUtil.BASE_CHAR;
            }
            codeGenerator = new RandomGenerator(baseStr, captchaProperties.getCount());
        }
        return codeGenerator;
    }
}
